package com.diudiutechnology.www.yadiudiu.yadiudiu.car;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.diudiutechnology.www.yadiudiu.util.http.OkHttpClientManager;
import com.diudiutechnology.www.yadiudiu.util.http.User;
import com.diudiutechnology.www.yadiudiu.util.http.UserCallback;
import com.diudiutechnology.www.yadiudiu.util.utils.Constant;
import com.diudiutechnology.www.yadiudiu.util.utils.CustomLayout;
import com.diudiutechnology.www.yadiudiu.util.utils.Sundry;
import com.diudiutechnology.www.yadiudiu.util.view.ListViewForScrollView;
import com.diudiutechnology.www.yadiudiu.util.view.MyScrollView;
import com.diudiutechnology.www.yadiudiu.yadiudiu.car.MyActivity.CarShopActivity;
import com.diudiutechnology.www.yadiudiu.yadiudiu.main.LazyFragment;
import com.diudiutechnology.www.yadiudiu.yadiudiu.singlecall.MyActivity.SearchActivity;
import com.diudiutechnology.www.yadiudiu.yadiudiu.singlecall.MyActivity.CoolieActivity;
import com.diudiutechnology.www.yadiudiu.yadiudiu.car.MyActivity.ShopForDetailsActivity;
import com.diudiutechnology.www.yadiudiu.yadiudiu.car.MyActivity.VehicleInsuranceActivity;
import com.diudiutechnology.www.yadiudiu.yadiudiu.car.MyActivity.WashTheCarMapActivity;
import com.diudiutechnology.www.yadiudiu.yadiudiu.mine.MyAdapter.BannersAdapter;
import com.diudiutechnology.www.yadiudiu.yadiudiu.car.MyAdapter.BuyACarDoorFragmentAdapter;
import com.diudiutechnology.www.yadiudiu.yadiudiu.mine.MyAdapter.BuyACarFragmentAdapter;
import com.diudiutechnology.www.yadiudiu.yadiudiu.model.BuyACarFragmentModel;
import com.diudiutechnology.www.yadiudiu.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import okhttp3.Call;

/**
 * Created by Administrator on 2017/8/14.
 * 购车板块首页
 */

public class BuyACarFragment extends LazyFragment implements View.OnClickListener ,MyScrollView.Callbacks{
    private Context context;
    //购车
    private RelativeLayout viewpager_title_all;//购车
    private ImageView all_xiala;

    private  RelativeLayout wash_the_car;//洗车
    private  ImageView wash_the_car_img;
    private  LinearLayout retur;//洗车地图

    private  RelativeLayout on_the_door;//上户
    private  ImageView on_the_door_img;
    BuyACarDoorFragmentAdapter doorFragmentAdapter;

    private  RelativeLayout car_insurance;//车险

    private TextView user_fragment_title;

    ListViewForScrollView buyacar_list;//listview
//    private List<BuyACarFragmentModel> buycarlist;

    private LinearLayout search_ly;//搜索

    private CustomLayout customLayout;//轮播布局
    private ViewPager ad_pager;
    private LinearLayout ad_layer; // 圈圈 布局
    private final int HOME_AD_RESULT = 1;
    private Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                // 广告
                case HOME_AD_RESULT:
                    ad_pager.setCurrentItem(ad_pager.getCurrentItem() + 1,
                            true);
                    break;
            }
        };
    };
    int pos=0;//洗车，购车
    BuyACarFragmentAdapter adapter;
    MyScrollView scroll_view;
    private LinearLayout car_layout;
    LinearLayout fixed_position;//固定car_layout位置
    int scroll_viewpos=0;//点击按钮MyScrollView回到坐标 0，0

    ///////////////////////////////////////
    private HashMap<String, String> map = new HashMap<String, String>();

    @Override
    protected void onCreateViewLazy(Bundle savedInstanceState) {
        super.onCreateViewLazy(savedInstanceState);
        setContentView(R.layout.fragment_buy_a_car);
        initView();
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context=getActivity();
    }
    //初始化View
    private void initView(){
        retur= (LinearLayout) findViewById(R.id.retur);
        fixed_position= (LinearLayout) findViewById(R.id.fixed_position);
        user_fragment_title= (TextView) findViewById(R.id.user_fragment_title);
        customLayout= (CustomLayout) findViewById(R.id.customLayout);
        ad_pager = (ViewPager)findViewById(R.id.ad_pager);
        ad_layer = (LinearLayout)findViewById(R.id.ad_layer);
        buyacar_list= (ListViewForScrollView)findViewById(R.id.buyacar_list);
        viewpager_title_all= (RelativeLayout) findViewById(R.id.viewpager_title_all);
        all_xiala= (ImageView) findViewById(R.id.all_xiala);
        wash_the_car= (RelativeLayout) findViewById(R.id.wash_the_car);
        wash_the_car_img= (ImageView) findViewById(R.id.wash_the_car_img);
        on_the_door= (RelativeLayout) findViewById(R.id.on_the_door);
        on_the_door_img= (ImageView) findViewById(R.id.on_the_door_img);

        car_insurance= (RelativeLayout) findViewById(R.id.car_insurance);

        search_ly= (LinearLayout)findViewById(R.id.search_ly);
        scroll_view= (MyScrollView) findViewById(R.id.scroll_view);
        car_layout= (LinearLayout)findViewById(R.id.car_layout);


        scroll_view.setCallbacks(this);
        scroll_view.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                System.out.println("scroll_viewpos"+scroll_viewpos);
                if (scroll_viewpos==1){
                    scroll_view.scrollTo(0,0);
                    scroll_viewpos=0;
                }
                    onScrollChanged(scroll_view.getScrollY());
            }
        });
        viewpager_title_all.setOnClickListener(this);
        wash_the_car.setOnClickListener(this);
        on_the_door.setOnClickListener(this);
        car_insurance.setOnClickListener(this);
        search_ly.setOnClickListener(this);
        retur.setOnClickListener(this);
        List<String> getinforresult=new ArrayList<>();
        getinforresult.add("1");
        getinforresult.add("1");
        getinforresult.add("1");
        getadvertising(getinforresult.size(),getinforresult);

//        processadapter();
        buyacar_list.setOnItemClickListener(clickListener);

        ////////////////////////////////////////////////
        onShop("106.549193","29.565497","1");
    }
AdapterView.OnItemClickListener clickListener=new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        if(pos==0){
            startActivity(new Intent(context, CarShopActivity.class));
           // startActivity(new Intent(context, WashCarDetailsActivity.class));
        }else if(pos==1){
            startActivity(new Intent(context, ShopForDetailsActivity.class));

        }  else if(pos==2){
            Intent intent=  new Intent();
            intent.setClass(context,CoolieActivity.class);//上户
        intent.putExtra("pos","2");
            startActivity(intent);
        }

    }
};

    //初始化adapter
    public void processadapter() {
        if (pos ==2) {
            doorFragmentAdapter= new BuyACarDoorFragmentAdapter(context);
            buyacar_list.setAdapter(doorFragmentAdapter);
        } else {
            adapter=new BuyACarFragmentAdapter(context);
            buyacar_list.setAdapter(adapter);
        }
//        initData();
    }
//数据
//private void  initData(){
//    buycarlist=new ArrayList<>();
//    if (pos==0){
//        buycarlist.clear();
//        BuyACarFragmentModel model=new BuyACarFragmentModel();
//        model.setName("东风标致4S店重庆店");
//        model.setStar("2");
//        model.setLet("标致508");
//        model.setMoney1("29.67");
//        model.setMoney2("25.00");
//        model.setDistance("2.58");
//        model.setDate("20");
//        model.setPraise("200");
//        buycarlist.add(model);
//        model=new BuyACarFragmentModel();
//        model.setName("广汽传祺广汽传祺广汽传祺祺");
//        model.setStar("3");
//        model.setLet("传祺GS8");
//        model.setMoney1("26.97");
//        model.setMoney2("25.00");
//        model.setDistance("3.25");
//        model.setDate("1.5万");
//        model.setPraise("1.8万");
//        buycarlist.add(model);
////        adapter.addItem(buycarlist,0);
//    }else if (pos==1){
//        buycarlist.clear();
//        BuyACarFragmentModel model=new BuyACarFragmentModel();
//        model.setName("重庆南岸白鹤路店");
//        model.setStar("2");
//        model.setLet("标准洗车");
//        model.setMoney1("25");
//        model.setMoney2("20");
//        model.setDistance("2.25");
//        model.setDate("20");
//        model.setPraise("200");
//        buycarlist.add(model);
//        model=new BuyACarFragmentModel();
//        model.setName("重庆南岸白鹤路店");
//        model.setStar("2");
//        model.setLet("标准洗车");
//        model.setMoney1("25");
//        model.setMoney2("20");
//        model.setDistance("2.25");
//        model.setDate("1.5万");
//        model.setPraise("1.5万");
//        buycarlist.add(model);
//        model=new BuyACarFragmentModel();
//        model.setName("重庆南岸白鹤路店");
//        model.setStar("2");
//        model.setLet("标准洗车");
//        model.setMoney1("25");
//        model.setMoney2("20");
//        model.setDistance("3.21");
//        model.setDate("20");
//        model.setPraise("200");
//        buycarlist.add(model);
////        adapter.addItem(buycarlist,1);
//    }else {
//        buycarlist.clear();
//        BuyACarFragmentModel model=new BuyACarFragmentModel();
//        model.setName("洁净干洗店");
//        model.setLet("百年老店，品质服务，绝对正宗干洗，用心服务，给您完美体验。");
//        model.setDistance("2.25");
//        model.setDate("20");
//        model.setPraise("200");
//        buycarlist.add(model);
////        doorFragmentAdapter.addItem(buycarlist);
//    }
//}
    //轮播广告
    public void getadvertising(final int size, final List<String> getinforresult){
        ad_pager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int position) {
                Sundry.refreshPoint(ad_layer,position % size,0);
                if (mHandler.hasMessages(HOME_AD_RESULT)) {
                    mHandler.removeMessages(HOME_AD_RESULT);
                }
                mHandler.sendEmptyMessageDelayed(HOME_AD_RESULT,  3*1000);
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
                if (ViewPager.SCROLL_STATE_DRAGGING == arg0
                        && mHandler.hasMessages(HOME_AD_RESULT)) {
                    mHandler.removeMessages(HOME_AD_RESULT);
                }
            }
        });
        BannersAdapter adapter = new BannersAdapter(context, getinforresult,0);
        ad_pager.setAdapter(adapter);
        Sundry.initPointsLayout(ad_layer,context,size,0);
        ad_pager.setCurrentItem(size * 1000, false);
        // 自动轮播线程
        mHandler.sendEmptyMessageDelayed(HOME_AD_RESULT, 3*1000);
    }
    @Override
    public void onClick(View view) {
switch (view.getId()){
    case  R.id.viewpager_title_all:
        pos=0;
        all_xiala.setVisibility(View.VISIBLE);
        wash_the_car_img.setVisibility(View.GONE);
        on_the_door_img.setVisibility(View.GONE);
        retur.setVisibility(View.GONE);
        user_fragment_title.setText(getResources().getString(R.string.buyacar));
//        processadapter();
        onShop("106.549193","29.565497","1");
        scroll_viewpos = 1;

        break;
    case R.id.wash_the_car:
        pos=1;
        all_xiala.setVisibility(View.GONE);
        wash_the_car_img.setVisibility(View.VISIBLE);
        on_the_door_img.setVisibility(View.GONE);
        retur.setVisibility(View.VISIBLE);
        user_fragment_title.setText(getResources().getString(R.string.washthecar));
//        processadapter();
        scroll_viewpos = 1;

        break;
    case R.id.on_the_door:
        pos=2;
        all_xiala.setVisibility(View.GONE);
        wash_the_car_img.setVisibility(View.GONE);
        on_the_door_img.setVisibility(View.VISIBLE);
        retur.setVisibility(View.GONE);
        user_fragment_title.setText(getResources().getString(R.string.onthedoor));

//        processadapter();
        scroll_viewpos = 1;
        break;
    case R.id.search_ly:
        startActivity(new Intent(context, SearchActivity.class));
    break;
    case R.id.car_insurance:
        startActivity(new Intent(context, VehicleInsuranceActivity.class));
        break;
    case R.id.retur:
        startActivity(new Intent(context, WashTheCarMapActivity.class));
        break;

}
}
    //scrollview初始位置
    public void processscrollTo(){
        if (scroll_view!=null){
            scroll_viewpos=1;
        }
    }
    @Override
    public void onScrollChanged(int scrollY) {
        car_layout.setTranslationY(Math.max(fixed_position.getTop(), scrollY));
       // scroll_viewpos=0;
    }

    @Override
    public void onDownMotionEvent() {

    }

    @Override
    public void onUpOrCancelMotionEvent() {

    }
    ///////////////////////////////////////////////////////////////////////////////
    public void onShop(String longitude,String latitude,String p){
        Log.e("myHOme","成功@@@@@@@@@@@@@@!!!!!!!!!!!");
        map.put("longitude",longitude);//经度
        map.put("latitude",latitude);//纬度
        map.put("p",p);//页码
        OkHttpClientManager.getInstance(getContext()).postUser(Constant.SHOP, map, new UserCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {

                Log.e("myHOme",e.getMessage()+"@@@@@@@@@@@@@@");

            }

            @Override
            public void onResponse(User response, int id) {

                if (response.error.equals("200")&&response.error.toString()=="200"){
                    Log.e("myHOme","成功@@@@@@@@@@@@@@");
                    adapter=new BuyACarFragmentAdapter(context);
                    buyacar_list.setAdapter(adapter);
                }else {

                    Log.e("myHOme","失败@@@@@@@@@@@@@@"+response.mssage+"!!!!!!!!!!"+response.error);
                }
            }
        });

    }
}
